<template>
	<div>
		<group>
			<x-input title="会员名称" label-width="70px" v-model="form.vip_name" placeholder="请输入名称"></x-input>
			<x-input title="手 机 号" label-width="70px" name="mobile" v-model="form.mobile" placeholder="请输入手机号码" keyboard="number" is-type="china-mobile"></x-input>
			<x-input title="密　　码" label-width="70px" type="password" v-model="form.password" placeholder="请输入密码"></x-input>
			<x-input title="确认密码" label-width="70px" type="password" v-model="form.password_confirm" placeholder="请再次输入密码"></x-input>
			<x-input title="验 证 码" label-width="70px" class="weui-vcode" placeholder="请输入验证码" v-model="form.smscode">
				<x-button v-if="enter_code == 0" slot="right" @click.native="get_code" type="primary" mini>发送验证码</x-button>
				<x-button v-else slot="right" type="primary" disabled mini>{{enter_code}}秒后重发</x-button>
			</x-input>
		</group>
		<box gap="20px 10px">
			<x-button type="primary" :show-loading="loading" @click.native="submit">注册</x-button>
			<p style="text-align:center;color:#409EFF;font-size:12px; padding:10px 0;"><span @click="$router.push({name:'login'})">去登录</span></p>
		</box>
	</div>
</template>

<script>
	import allapi from '@/http';
  import cookie from 'js-cookie';
	export default {
		components:{

		},
		data(){
			return {
				enter_code : 0,
				loading : false,
				form : {
					vip_name : '',
					mobile : '',
					password : "",
					password_confirm : "",
					smscode : ''
				}
			}
		},
		methods : {
			get_code(){
				var time;
				if(this.form.mobile == ''){
					return this.$message({
						message: '手机号码不能为空',
						type: 'error'
					});
				}
        this.$http.setting.post('/sms/send/register/' + this.form.mobile,{}).then(res => {
					if(!res.data.code){
						this.$message({
							message: '发送成功,请留意手机短信!',
							type: 'success'
						});
						this.enter_code = 60;
						time = setInterval(()=>{
							this.enter_code --;
							if(this.enter_code == 0){
								clearInterval(time)
							}
						},1000)
					}else{
						this.$message({
							message: res.data.message,
							type: 'error'
						});
					}
				});
			},
			submit(){
				if(this.form.mobile == ''){
					return this.$message({
						message: '手机号码不能为空',
						type: 'error'
					});
				}
				this.$http.passport.post('/vip/'+cookie.get('tanantId')+'/addnew',this.form).then(res => {
					if(!res.data.code){
						this.$message({
							message: '注册成功',
							type: 'success'
						});
						// localStorage.setItem('user', res.data.data.user);
						// localStorage.setItem('company', res.data.data.company);
						// localStorage.setItem('api_token', res.data.data.api_token);
						// localStorage.setItem('refresh_token', res.data.data.refresh_token);
						// allapi.forEach(item => {
						// 	item.defaults.headers.common['Authorization'] = 'Bearer ' + res.data.data.api_token;
						// })
						this.$router.push({
							name: 'login'
						});
					}else{
						this.$message({
							message: '注册失败!'+res.data.message,
							type: 'error'
						});
					}
				})
			}
		},
		computed : {

		},
		created(){

		},
		watch : {

		}
	}
</script>
